package com.gobrs.async.core.task;

import com.gobrs.async.core.common.domain.AnyConditionResult;
import com.gobrs.async.core.common.domain.TaskResult;
import com.gobrs.async.core.common.enums.ResultState;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

public class TaskUtil {

    public static boolean multipleDependencies(Map<AsyncTask, List<AsyncTask>> upwardTasksMap, List<AsyncTask> subTasks) {
        for (AsyncTask subTask : subTasks) {
            if (!CollectionUtils.isEmpty(upwardTasksMap.get(subTask)) && upwardTasksMap.get(subTask).size() > 1) {
                return true;
            }
        }
        return false;
    }

    public static AnyConditionResult defaultAnyCondition() {
        return AnyConditionResult.builder().setState(false).build();
    }

    public static AnyConditionResult defaultAnyCondition(boolean state) {
        return AnyConditionResult.builder().setState(state).build();
    }

    public static TaskResult buildFutureTaskResult(Future result) {
        return new TaskResult(result, ResultState.SUCCESS, null);
    }

}
